<?php
try{
	include '../ini.php';
    include '../classes/xl_product_categories.php';
    $dt_xl_product_category=new xl_product_category;
   
    if(empty($_GET['id'])){
        throw new Exception('Không tồn tại mã này');
    }
    $read_cate=$dt_xl_product_category->read($_GET['id']);
    if($read_cate === false){
        throw new Exception('Không tồn tại mã này');
    }
    if($_GET['id'] != $read_cate['id']){
        $_GET['id']=$dt_xl_admin->removeSQLI($_GET['id']);
        header("Location: categories_update.php?id=".$_GET['id']);
    }
    $read_list=$dt_xl_product_category->read_list();
    function print_list($list,$id,$characters,$id_select,$id_hidden){
        $str='';
    	foreach ($list as $location){
    		if ($location['parent'] == $id):
                if($location['id'] != $id_hidden){
        			if($location['id'] == $id_select){		
        				$str.= '<option selected="selected" value="'.$location['id'].'">'.$characters.$location['name'].'</option>';
        			} else{
        				$str.= '<option value="'.$location['id'].'">'.$characters.$location['name'].'</option>';
        			}
                }
    		    $str.= print_list($list, $location['id'], $characters.$characters, $id_select,$id_hidden);
    		endif;
        };
        return $str;
    }
    $dt_smarty->assign('location',count($read_list));
    $dt_smarty->assign('read_cate',$read_cate);
    $dt_smarty->assign('read_list',$read_list);
	$contenForLayout=$dt_smarty->fetch('product/categories_update.tpl');
	$dt_smarty->assign('contenForLayout',$contenForLayout);
	$dt_smarty->assign('titleForLayout','Chỉnh sữa thể loại');
	$dt_smarty->display('layout/defaults.tpl');
    $dbh=NULL;
	
} catch(Exception $e){
	$_SESSION['msg']=$e->getMessage();
    $_SESSION['type_msg']='error';
    if(empty($_SERVER['HTTP_REFERER'])){
        header('Location: categories_list.php');
    }else{
        header('Location:'.$_SERVER['HTTP_REFERER']);
    }
	$dbh=NULL;
}